import { outputDir, plugins, rules, resolve, babelPlugin, babelPreset, babelOptions } from "./base";
import { optimize, Configuration, EnvironmentPlugin } from "webpack";
import * as path from "path";

const cfg: Configuration = {
  entry: {
    view: "./src/view/index.tsx",
    edit: ["./src/edit/index.tsx"]
  },
  output: {
    path: outputDir,
    filename: "[name].js"
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js", ".jsx"]
  },
  module: {
    rules: [
      ...Object.values(rules),
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: "awesome-typescript-loader",
            options: {
              silent: true,
              useBabel: true,
              babelOptions: {
                babelrc: false,
                plugins: [],
                presets: babelOptions.presets
              },
              babelCore: resolve("@babel/core")
            }
          }
        ]
      },
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        exclude: (p: string) => {
          return /node_modules/.test(p) && !/x-site-ui|plume2/.test(p);
        },
        options: {
          babelrc: false,
          plugins: [],
          presets: babelOptions.presets
        }
      },
      {
        test: new RegExp(`src\\${path.sep}(view|edit)\\${path.sep}index\\.tsx?$`),
        loader: path.resolve(__dirname, "../loader/normalize")
      }
    ]
  },
  resolveLoader: {
    modules: module.paths
  },
  plugins: [
    plugins.extractText,
    new EnvironmentPlugin({
      NODE_ENV: "production",
      DEBUG: false
    })
  ]
};

export default cfg;
